package com.mall.controller;

import java.io.InputStream;
import java.net.URI;
import java.rmi.server.ObjID;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jcraft.jsch.UserInfo;
import com.mall.bean.Blog;
import com.mall.bean.Goods;
import com.mall.bean.User;
import com.mall.service.IBlogService;

@Controller
public class BlogController {

	@Resource
	private IBlogService blogService;

	// 跳转博客1
	@RequestMapping("blog-1")
	public String blog1(Model model, Blog blog, HttpSession session) {

		List<Blog> allBlog = blogService.findAllBlog(blog);
		model.addAttribute("allBlog", allBlog);
		return "blog-1";
	}

	// 跳转博客2
	@RequestMapping("/blog-2")
	public String blog2(Model model, Blog blog, HttpSession session) {
		//User user = (User) session.getAttribute("user");
		User user = UserController.userInfo(session);
		model.addAttribute("user", user);
		List<Blog> allBlog = blogService.findAllBlog(blog);
		model.addAttribute("allBlog", allBlog);
		System.out.println("success");
		return "blog-2";
	}

	//展示买家秀或者卖家秀
	@RequestMapping("showBSBlog")
	@ResponseBody
	public Map<String, Object> showBSBlog(@Param("isSeller") String isSeller, Model model, Blog blog) {

		Map<String, Object> resMap = new HashMap<String, Object>();
		blog.setIsSeller(isSeller);
		List<Blog> bs = blogService.findBSBlog(blog);
		resMap.putIfAbsent("bs", bs);
		return resMap;
	}


	//发表博客
	@RequestMapping("addBlog")
	public String addBlog(@RequestParam("url") MultipartFile[] file, HttpSession session, Blog blog, Model model) throws Exception {

		User user = (User) session.getAttribute("user");

		System.out.println("blog：" + blog);
		List<String> arrary = new ArrayList<>();
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		String[] list = nowdata.split(" ");
		String data = list[0] + "-" + list[1].split(":")[0] + list[1].split(":")[1] + list[1].split(":")[2];

		System.out.println("开始循环咯" + data);


		//生成不重复的随机set集合
		for (int i = 0; i < file.length; i++) {
			int n = 1;
			Set set = new HashSet();
			Random random = new Random();
			while (set.size() < n) {
				set.add(random.nextInt(100000000));
			}
			Iterator it = set.iterator();
			while (it.hasNext()) {
				Object number = it.next();
				System.out.println("开始循环咯");
				String url = file[i].getOriginalFilename();
				String type = file[i].getContentType().split("/")[1];
				String newurl = null;


				if ("jpeg".equals(type) || "jpg".equals(type) || "png".equals(type) || "bmp".equals(type) || "webq".equals(type) || "mp4".equals(type)) {
					newurl = "hdfs://iflytek002:9000/photo/" + user.getUsername() + "-" + data + "-" + number + "." + type;
					System.out.println("newurl：" + newurl);
					arrary.add(newurl);
					System.out.println("array：" + arrary.get(0));

					if ("mp4".equals(type)) {
						blog.setVideo(arrary.get(i));
					}
					if (i == 0) {
						blog.setUrl1(arrary.get(0));
					} else if (i == 1) {
						blog.setUrl2(arrary.get(1));
					} else if (i == 2) {
						blog.setUrl3(arrary.get(2));
					} else if (i == 3) {
						blog.setUrl4(arrary.get(3));
					} else if (i == 4) {
						blog.setUrl5(arrary.get(4));
					} else if (i == 5) {
						blog.setUrl6(arrary.get(5));
					} else if (i == 6) {
						blog.setUrl7(arrary.get(6));
					} else if (i == 7) {
						blog.setUrl8(arrary.get(7));
					} else if (i == 8) {
						blog.setUrl9(arrary.get(8));
					}
				}
			}
		}
		System.out.println("第一次上传：" + arrary + "长度：" + arrary.size());
		upload(file, arrary);
		blog.setUser_id(user.getUser_id());
		String[] dt = nowdata.split(" ")[0].split("-");
		String date = dt[1] + "月" + dt[2] + "日";
		blog.setDate(nowdata);
		blog.setMonth(date);
		blogService.addBlog(blog);
		System.out.println("上传成功；" + arrary + "长度：" + arrary.size());
		List<Blog> allBlog = blogService.findAllBlog(blog);
		model.addAttribute("allBlog", allBlog);

		return "blog-2";
	}

	//上传图片视频
	private void upload(@RequestParam("url") MultipartFile[] file, List<String> url) throws Exception {
		Configuration conf = new Configuration();
		// int length = file.length;
		for (int i = 0; i < file.length; i++) {
			InputStream in = file[i].getInputStream();
			URI hfdfsfile = new URI("hdfs://iflytek002:9000");
			conf.set("fs.defaultFS", "hdfs://iflytek002:9000");
			conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
			/* conf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem"); */
			FileSystem fs = FileSystem.get(hfdfsfile, conf, "root");
			Path path = new Path(url.get(i));
			FSDataOutputStream out = fs.create(path);
			IOUtils.copyBytes(in, out, conf);
			fs.close();
			in.close();
		}
	}


}
